package com.hooda.framework.orm;

import com.hooda.framework.annotations.Autowired;
import com.hooda.framework.annotations.Component;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * @author zhangzp
 * @version 1.0, 2021/8/12
 */
@Component
public class ConnectionManager {

    @Autowired
    private DruidManager druidManager;

    private ThreadLocal<Connection> connectionThreadLocal = new ThreadLocal<>();

    public ConnectionManager() {
    }

    public Connection getCurrentThreadConnection() throws SQLException {
        Connection connection = connectionThreadLocal.get();
        if (connection != null) {
            return connection;
        }
        connection = druidManager.getConnection();
        connectionThreadLocal.set(connection);
        return connection;
    }
}
